﻿using CommonEntity.CommonImageEntities;
using Google.Protobuf;
using OpenCvSharp;
using ScienceRpcServe.Protos;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ScienceAppService.RpcUtils;
public static class ProtoConvert
{
    public static ByteString ConvertToByteString(this Mat? mat)
    {
        if (mat == null) return ByteString.Empty;
        mat.ConvertTo(mat, OpenCvSharp.MatType.CV_8UC1);
        return ByteString.CopyFrom(mat.ToBytes());
    }

    public static ImageInfo ToImageInfo(this ImageInfoResponse infoRes)
    {
        var imageInfo = new ImageInfo
        {
            Count = infoRes.Count,
            Width = infoRes.Width,
            Height = infoRes.Height,
            ImageFactor = infoRes.ImageFactor,
            OriginWindowMax = infoRes.OriginWindowMax,
            OriginWindowMin = infoRes.OriginWindowMin,
            WinEnd = infoRes.SliceMax,
            WinStart = infoRes.SliceMin
        };

        return imageInfo;
    }

    public static IList<SplitSeriesInfo> ToSplitSeriesInfoList(this SplitResponse spRes)
    {
        return spRes.SplitInfos.Select(si => si.ToSplitSeriesInfo()).ToList();
    }

    public static SplitSeriesInfo ToSplitSeriesInfo(this SplitInfo si)
    {
        var spi = new SplitSeriesInfo
        {
            PatientName = si.PatientName,
            Width = si.Width,
            Height = si.Height,
            FCount = si.Count,
            PatientId = si.PatientId,
            SeriesDescription = si.SeriesDescription,
            SeriesTime = si.SeriesTime,
            StudyDescription = si.StudyDescription,
            StudyUid = si.StudyUid,
            Key = si.Key
        };
        return spi;
    }

    public static Mat? ConvertToMat(this ByteString bytes)
    {
        if (bytes.IsEmpty) return null;
        var imageData = bytes.ToByteArray();
        using var ms = new MemoryStream(imageData);
        return Mat.FromStream(ms, ImreadModes.Color);
    }

    public static Mat? ConvertToGreyMat(this ByteString bytes)
    {
        if (bytes.IsEmpty) return null;
        var imageData = bytes.ToByteArray();
        using var ms = new MemoryStream(imageData);
        return Mat.FromStream(ms,ImreadModes.Grayscale);
    }

    public static ImageOrientation ConvertToProtoOrientation(this ImageOrientationEnum orientation)
    {
        return orientation switch
        {
            ImageOrientationEnum.TransversePlane => ImageOrientation.TransversePlane,
            ImageOrientationEnum.SagittalPlane => ImageOrientation.SagittalPlane,
            ImageOrientationEnum.CoronalPlane => ImageOrientation.CoronalPlane,
            ImageOrientationEnum.ThreeD => ImageOrientation.ThreeD
        };
    }

    public static ImageOrientationEnum ConvertToImageOrientation(this ImageOrientation orientation)
    {
        return orientation switch
        {
            ImageOrientation.TransversePlane => ImageOrientationEnum.TransversePlane,
            ImageOrientation.SagittalPlane => ImageOrientationEnum.SagittalPlane,
            ImageOrientation.CoronalPlane => ImageOrientationEnum.CoronalPlane,
            ImageOrientation.ThreeD => ImageOrientationEnum.ThreeD
        };
    }

}
